# 输入目录页面，获取所有章节，并保存
import requests
from lxml import etree
import chardet
import tool.tool

# 获取章节列表
header = {
    'Connection': 'Keep-Alive',
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0'
}
r = requests.get("https://www.westnovel.com/wuxia/ssjxz/", headers=header)
r.encoding = chardet.detect(r.content)['encoding']
html = etree.HTML(r.text)
print("html get success")
# 章节名称
chapter_title_arr = html.xpath("//dl[@class='chapterlist']/dd/a/@title")
chapter_url_arr = html.xpath("//dl[@class='chapterlist']/dd/a/@href")
novel_title_arr = html.xpath("//div[@class='btitle']/h1/a/text()")
novel_title = novel_title_arr[0]
direction_file_path = "/Users/liuqingyuan/Downloads/小说/%s" % novel_title
tool.tool.create_direction(direction_file_path)
title_str = ""
for index in range(len(chapter_url_arr)):
    print("标题是%s,详情是%s" % (chapter_title_arr[index], chapter_url_arr[index]))
    if index == 0:
        title_str = chapter_title_arr[index]
    else:
        title_str += "\n" + chapter_title_arr[index]
    # 获取小说内容
    content = requests.get("https://www.westnovel.com/%s" % chapter_url_arr[index], headers=header)
    content.encoding = chardet.detect(content.content)['encoding']
    content_html = etree.HTML(content.text)
    content_arr = content_html.xpath("//div[@id='BookText']/p/text()")
    if len(content_arr) == 0:
        content_arr = content_html.xpath("//div[@id='BookText']/text()")
    # 将内容拼接保存在txt文件中
    content_str = ''
    for item in content_arr:
        content_str += "%s\n" % item
    file_path = "/Users/liuqingyuan/Downloads/小说/%s/%d-%s.txt" % (novel_title, index, chapter_title_arr[index])
    tool.tool.createFile(file_path)
    tool.tool.writeFile(file_path, content_str)
title_file_path = "/Users/liuqingyuan/Downloads/小说/%s/目录.txt" % novel_title
tool.tool.createFile(title_file_path)
tool.tool.writeFile(title_file_path, title_str)
